﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
using umbraco.editorControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using umbraco.NodeFactory;
using System.Web.SessionState;
using umbraco.BusinessLogic.Actions;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic.property;
using umbraco.interfaces;
using ExtendedWorkFlow.Helpers;
using ExtendedWorkFlow.Commands;

namespace ExtendedWorkFlow
{
    public class WorkFlowActionWatcher : umbraco.BusinessLogic.Actions.IActionHandler
    {

        String umbraco.BusinessLogic.Actions.IActionHandler.HandlerName()
        {
            // return the name of the custom action handler
            return "WorkFlowHelper.ActionWatcher";
        }

        umbraco.interfaces.IAction[] umbraco.BusinessLogic.Actions.IActionHandler.ReturnActions()
        {            
            // return the actions for which this action handler is used
            return new IAction[] { ActionToPublish.Instance };
        }

        Boolean umbraco.BusinessLogic.Actions.IActionHandler.Execute(Document Document, IAction Action)
        {
            // IWorkFlowCommand objIWorkFlowCommand = WorkFlowHelper.GetRunCommand("ExtendedWorkFlow.Commands.Email, ExtendedWorkFlow");

            Node objNode = Functions.GetNodeFromPath("/WorkFlow");

            // Check the workflow group exists
            if (objNode.Id != 0 &&
                objNode.NodeTypeAlias == "Groups")
            {
                DocumentType objDocumentType = GetDocumentRootDocumentType(Document);

                // Loop though all workflow to find if any match the document type
                foreach (Node objWorkflow in objNode.Children)
                {
                    if (objWorkflow.GetProperty("documentTypes") != null)
                        if (objWorkflow.GetProperty("documentTypes").Value.Contains(objDocumentType.Id.ToString()))
                        {
                            // Fire event to start the new work flow process
                            WorkFlow objWorkFlow = WorkFlow.MakeNew(Document, objWorkflow);                            
                        }
                }

            }

            return true;
        }

        /// <summary>
        /// Get the root document type for a document
        /// </summary>
        /// <param name="Document">Document to get the root document type for</param>
        /// <returns>DocumentType root</returns>
        DocumentType GetDocumentRootDocumentType(Document Document)
        {
            DocumentType objDocumentType = new DocumentType(Document.ContentType.Id);

            // Loop though all document types to get the parent
            while (objDocumentType.MasterContentType != 0)
                objDocumentType = new DocumentType(objDocumentType.MasterContentType);

            return objDocumentType;
        }
        
    }
}
